package controller.business.observerReport;

import com.lowagie.text.*;
import com.lowagie.text.rtf.RtfWriter2;
import itext.ItextFont;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

public class ObserverReportDoc {
    public static void createDoc(OutputStream fileOutputStream, Map<String, String> map) throws DocumentException, IOException {
        //设置纸张大小
        Document document = new Document(PageSize.A4, 90.14F, 90.14F, 72F, 72F);
        //建立一个书写器，与document对象关联
        RtfWriter2.getInstance(document, fileOutputStream);
        document.open();


        Paragraph paragraph = new Paragraph( map.get("title"), ItextFont.fangZhengDoc18);
        paragraph.setAlignment(Element.ALIGN_CENTER);
        document.add(paragraph);

        paragraph = new Paragraph("区县市场监管局（盖章）：", ItextFont.fangSongNormalDoc12);
        paragraph.setAlignment(Element.ALIGN_LEFT);
        paragraph.setSpacingBefore(15);
        document.add(paragraph);

        paragraph = new Paragraph("观察员（签字）：" , ItextFont.fangSongNormalDoc12);
        paragraph.setAlignment(Element.ALIGN_LEFT);
        paragraph.setSpacingBefore(15);
        document.add(paragraph);


        //设置Table表格,创建一个7列的表格
        Table table = new Table(5);
        float width[] = {1.2F, 1.3F, 1, 1, 1};//设置每列宽度比例
        table.setWidths(width);
        table.setWidth(100);//占页面宽度比例
        table.setAlignment(Element.ALIGN_CENTER);//居中
        table.setAlignment(Element.ALIGN_MIDDLE);//垂直居中
        table.setAutoFillEmptyCells(true);//自动填满
        table.setBorderWidth(1);//边框宽度
        table.setPadding(15F);

        Cell[] cell = new Cell[24];
        cell[0] = new Cell(new Phrase("核查计划编号", ItextFont.fangSongNormalDoc12));
        cell[1] = new Cell(new Phrase( map.get("businessCode"), ItextFont.fangSongNormalDoc12));
        cell[1].setColspan(4);


        cell[2] = new Cell(new Phrase("企业名称", ItextFont.fangSongNormalDoc12));
        cell[3] = new Cell(new Phrase( map.get("companyName"), ItextFont.fangSongNormalDoc12));
        cell[3].setColspan(2);

        cell[4] = new Cell(new Phrase("申请类别", ItextFont.fangSongNormalDoc12));
        cell[5] = new Cell(new Phrase( map.get("businessType"), ItextFont.fangSongNormalDoc12));


        cell[6] = new Cell(new Phrase("产品名称", ItextFont.fangSongNormalDoc12));
        cell[7] = new Cell(new Phrase( map.get("productName"), ItextFont.fangSongNormalDoc12));
        cell[7].setColspan(2);
        cell[8] = new Cell(new Phrase("核查结果", ItextFont.fangSongNormalDoc12));
        cell[9] = new Cell(new Phrase( map.get("result"), ItextFont.fangSongNormalDoc12));


        cell[10] = new Cell(new Phrase("通知日期", ItextFont.fangSongNormalDoc12));
        cell[11] = new Cell(new Phrase(map.get("noticeTime"), ItextFont.fangSongNormalDoc12));
        cell[12] = new Cell(new Phrase("核查日期", ItextFont.fangSongNormalDoc12));
        cell[13] = new Cell(new Phrase(map.get("checkTime"), ItextFont.fangSongNormalDoc12));
        cell[13].setColspan(2);


        cell[14] = new Cell(new Phrase("审查组长", ItextFont.fangSongNormalDoc12));
        cell[15] = new Cell(new Phrase( map.get("leader"), ItextFont.fangSongNormalDoc12));
        cell[16] = new Cell(new Phrase("审查组成员", ItextFont.fangSongNormalDoc12));
        cell[17] = new Cell(new Phrase( map.get("crew"), ItextFont.fangSongNormalDoc12));
        cell[17].setColspan(2);


        cell[18] = new Cell(new Phrase("\n核查情况\n简述\n", ItextFont.fangSongNormalDoc12));
        cell[19] = new Cell(new Phrase( map.get("checkStatus"), ItextFont.fangSongNormalDoc12));
        cell[19].setColspan(4);


        cell[20] = new Cell(new Phrase("\n核查组纪律\n执行情况\n", ItextFont.fangSongNormalDoc12));
        cell[21] = new Cell(new Phrase( map.get("checkDiscipline"), ItextFont.fangSongNormalDoc12));
        cell[21].setColspan(4);

        cell[22] = new Cell(new Phrase("\n备注\n", ItextFont.fangSongNormalDoc12));
        cell[23] = new Cell(new Phrase( map.get("remark"), ItextFont.fangSongNormalDoc12));
        cell[23].setColspan(4);


        for (int i = 0; i < 24; i++) {

            if (i == 19 || i == 21 || i == 23) {
                cell[i].setHorizontalAlignment(Element.ALIGN_LEFT);//居中显示
                cell[i].setVerticalAlignment(Element.ALIGN_TOP);//纵向居中显示
            } else {
                cell[i].setHorizontalAlignment(Element.ALIGN_CENTER);//居中显示
                cell[i].setVerticalAlignment(Element.ALIGN_MIDDLE);//纵向居中显示
            }

            table.addCell(cell[i]);
        }

        paragraph = new Paragraph();
        paragraph.add(table);
        document.add(paragraph);

        paragraph = new Paragraph("注：1、此表由工业产品生产许可证观察员填写，相关信息要准确、真实。\n" +
                "　　2、此表一个企业填写一份，一式两份。其中，一份填好后应于核查结束后三日内寄送上海市质量技术监督局业务受理中心；" +
                "一份由区县市场监管局留。", ItextFont.fangSongNormalDoc12);

        document.add(paragraph);

        document.close();

    }

    public static void main(String[] args) {

        String file = "d:/test11.doc";
        Map map = new HashMap();
        map.put("title", "上海市工业产品生产许可证现场核查观察工作报告");
        map.put("observerName", "observerName");
        map.put("businessCode", "businessCode");
        map.put("companyName", "companyName");
        map.put("businessType", "businessType");
        map.put("productName", "productName");
        map.put("result", "result");
        map.put("noticeTime", "2017");
        map.put("checkTime", "2018");
        map.put("leader", "leader");
        map.put("crew", "crew");
        map.put("checkStatus", "checkStatus");
        map.put("checkDiscipline", "checkDiscipline");
        map.put("remark", "remark");


        try {
            OutputStream outputStream = new FileOutputStream(file);
            createDoc(outputStream, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


